﻿package sound 
{
	import flash.events.EventDispatcher;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.utils.Dictionary;
	
	/**
	 * ...
	 * @author ZhaoYun
	 */
	public class SoundManager extends EventDispatcher
	{
		private var soundDictionary:Dictionary;
		private var channelDictionary:Dictionary;
		private var soundTransform:SoundTransform;
		public function SoundManager() 
		{
			soundDictionary = new Dictionary();
			channelDictionary = new Dictionary();
			soundTransform = new SoundTransform(1);
		}
		
		private static var instance:SoundManager;
		
		public static function getInstance():SoundManager
		{
			if (instance == null)
				instance = new SoundManager();
			return instance;
		}
		
		public function registerSound(_name:String, _sound:Sound):void
		{
			soundDictionary[_name] = _sound;
		}
		
		public function playSound(_name:String,loop:int = 1):void
		{
			var tempSound:Sound = soundDictionary[_name];
			try 
			{
				if(tempSound != null)
					channelDictionary[tempSound] = SoundChannel(tempSound.play(0,loop,soundTransform));
			}
			catch (err:Error)
			{
				
			}
		}
		
		public function stopSound(_name:String):void
		{
			var tempSound:Sound = soundDictionary[_name];
			if (tempSound != null && channelDictionary[tempSound] != null)
				SoundChannel(channelDictionary[tempSound]).stop();
		}
		
		public function setVolume(value:int):void
		{
			soundTransform.volume = value;
		}
	}

}